﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Chat
{
    class ChatRoom
    {
        // Private data

        private List<String> m_users;
        private String m_roomName;
        private IMessageDelegate m_delegate;

        // Public interface

        public ChatRoom(String roomName, IMessageDelegate messageDelegate)
        {
            m_users = new List<String>();
            m_roomName = roomName;
            m_delegate = messageDelegate;
        }

        public String GetRoomName()
        {
            return m_roomName;
        }

        public void SendMessage(String sender, String message)
        {
            foreach (String user in m_users)
            {
                if (!user.Equals(sender))
                {
                    m_delegate.GotMessage(sender, user, "/room " + m_roomName + " " + message);
                }
            }
        }

        public void AddUser(String user)
        {
            Predicate<String> userIdEquals = (String userFromList) => { return userFromList.Equals(user); };
            if (m_users.Find(userIdEquals) == null)
            {
                m_users.Add(user);
            }
        }

        public void RemoveUser(String user)
        {
            m_users.Remove(user);
        }
    }
}
